package com.mycompany.project.client;

import com.bramosystems.oss.player.core.client.AbstractMediaPlayer;
import com.bramosystems.oss.player.core.client.LoadException;
import com.bramosystems.oss.player.core.client.PluginNotFoundException;
import com.bramosystems.oss.player.core.client.RepeatMode;
import com.bramosystems.oss.player.core.client.ui.NativePlayer;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.user.client.ui.AbsolutePanel;

public class VideoAnnotationPreview extends HorizontalPanel {
	private AbstractMediaPlayer previewVideo;
	private double playPosStart;
	private double playPosEnd;
	private Label labelDescrizione;
	public static final int TIMER_PREVIEW_SCHEDULE=300;
	private HandlerManager handleManagerOpenPreview; // handle manager for fire the OpenPrevieewEvent handler and consequently the onPreviewOpened event
	
	
	public VideoAnnotationPreview(String url, double startTime) {
		setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		try {
			handleManagerOpenPreview=new HandlerManager(null);// create the handle manager for dispatch the onPreviewOpened event when the user click annotation preview link 
			previewVideo= new NativePlayer(url,true, "100%", "100%");			
			previewVideo.addMouseDownHandler(new MouseDownHandler() {
				public void onMouseDown(MouseDownEvent event) {// On click open this preview on video player. fire the OpenPreview event.
					handleManagerOpenPreview.fireEvent(new OpenPreviewEvent(playPosStart)); //fire the OpenPreview event.
				}
			});
			previewVideo.setControllerVisible(false); // the default controls of the video should be hidden because hides a small 
			//previewVideo.pauseMedia();			
			previewVideo.setLoopCount(100);     // the video repeat 100 times after the end is reached
			previewVideo.setRepeatMode(RepeatMode.REPEAT_OFF);   // sets the playlist mode. Stop playback when end of playlist is reached
			//previewVideo.setVolume(0);				
			add(previewVideo);
			setCellVerticalAlignment(previewVideo, HasVerticalAlignment.ALIGN_MIDDLE);
			setCellHorizontalAlignment(previewVideo, HasHorizontalAlignment.ALIGN_CENTER);
			setCellWidth(previewVideo, "48%");
			setCellHeight(previewVideo, "100%");
			previewVideo.setSize("100%", "100%");
			this.setSize("100%", "50px");
			
			labelDescrizione = new Label("New Annotation Preview");
			labelDescrizione.addMouseOutHandler(new MouseOutHandler() {
				public void onMouseOut(MouseOutEvent event) {  // on mouse out set label color
					DOM.setStyleAttribute(labelDescrizione.getElement(), "color", "black"); //set label color black
				}
			});
			labelDescrizione.addMouseOverHandler(new MouseOverHandler() { //set 
				public void onMouseOver(MouseOverEvent event) { // on mouse over change the cursor and label color				
					DOM.setStyleAttribute(labelDescrizione.getElement(), "cursor", "pointer"); // set pointer
					DOM.setStyleAttribute(labelDescrizione.getElement(), "color", "blue"); //set label color blue
				}
			});
			labelDescrizione.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) { // On click open this preview on video player. fire the OpenPreview event.
					handleManagerOpenPreview.fireEvent(new OpenPreviewEvent(playPosStart)); // fire the OpenPreview event.
				}
			});
			
			AbsolutePanel absolutePanel = new AbsolutePanel();
			add(absolutePanel);
			setCellHeight(absolutePanel, "100%");
			setCellWidth(absolutePanel, "4%");
			labelDescrizione.setDirectionEstimator(false);
			labelDescrizione.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);			
			add(labelDescrizione);
			setCellHorizontalAlignment(labelDescrizione, HasHorizontalAlignment.ALIGN_CENTER);
			setCellVerticalAlignment(labelDescrizione, HasVerticalAlignment.ALIGN_MIDDLE);
			setCellHeight(labelDescrizione, "100%");
			setCellWidth(labelDescrizione, "48%");			
			playPosStart=startTime;
			playPosEnd=startTime+Annotation.ANNOTATION_DEFAULT_LIFE_TIME;			
			
			
			final Timer timerVideoPreviewControl=new Timer() {
				
				@Override
				public void run() {
					if(previewVideo.getPlayPosition()>=playPosEnd)
						previewVideo.setPlayPosition(playPosStart);
				}
			};
			
			
			
			Timer timerVideoLoaded=new Timer() {
				
				@Override
				public void run() {
					if(previewVideo.getMediaDuration()>0){
						this.cancel();
						previewVideo.setVolume(0);
						previewVideo.setPlayPosition(playPosStart);
						timerVideoPreviewControl.scheduleRepeating(TIMER_PREVIEW_SCHEDULE);
					}
					
				}
			};
			
			timerVideoLoaded.scheduleRepeating(TIMER_PREVIEW_SCHEDULE);
			
			//previewVideo.setPlayPosition(playPos);
		} catch (LoadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PluginNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	public void setDescriptionPreview(String description){
		labelDescrizione.setText(description);
	}
	
	public void setAnnotationPreviewStartTime(double val){
		playPosStart=val;
		previewVideo.setPlayPosition(playPosStart);
	}
	
	public void setAnnotationPreviewEndTime(double val){
		playPosEnd=val;
		previewVideo.setPlayPosition(playPosStart);
	}
	
	public void setPreviewTimeSegment(double timeValueStart,double timeValueEnd){
		setAnnotationPreviewStartTime(timeValueStart);
		setAnnotationPreviewEndTime(timeValueEnd);
	}	
	
	
	 /**
	 * This method add OpenPreviewEventHandler handler. This handler is for manage the
	 * opening of annotation preview
	 * @param handler: the OpenPreviewEventHandler handler
	 */
	public void addOpenPreviewHandler(OpenPreviewEventHandler handler){
		handleManagerOpenPreview.addHandler(OpenPreviewEvent.TYPE, handler);
	}
	

}
